//
//  NSMutableArray+HACShield.m
//  HACShield
//
//  Created by Chengjie on 2019/5/22.
//

#import "NSMutableArray+HACShield.h"
#import "NSObject+HACSwizzling.h"

@implementation NSMutableArray (HACShield)

/**
 NSMutableArray *commonArr = [NSMutableArray alloc];
 NSMutableArray *arr01 = [commonArr init];
 NSMutableArray *arr_copy0 = @[].mutableCopy;
 NSMutableArray *arr_copy1 = [commonArr initWithObjects:@"1", nil].mutableCopy;
 NSLog(@"%s--%s--%s--%s", object_getClassName(commonArr), object_getClassName(arr01), object_getClassName(arr_copy0), object_getClassName(arr_copy1));
 ----------------------------------
 2019-05-23 19:37:11.263702+0800 HACShield[19195:3674087] __NSPlaceholderArray--__NSArrayM--__NSArrayM--__NSArrayM
 ----------------------------------
 */
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 取值
        SEL sel_obj_at_index = @selector(objectAtIndex:);
        hac_swizzleInstanceMethod(hac_getClass(@"__NSA", @"rrayM"), sel_obj_at_index, @selector(hac_objectAtIndex:));
        // 添加出错
        SEL sel_add_obj = @selector(addObject:);
        hac_swizzleInstanceMethod(hac_getClass(@"__NSA", @"rrayM"), sel_add_obj, @selector(hac_addObject:));
        SEL sel_add_objs = @selector(addObjectsFromArray:);
        hac_swizzleInstanceMethod(hac_getClass(@"__NSA", @"rrayM"), sel_add_objs, @selector(hac_addObjectsFromArray:));
        // 插入
        SEL sel_insert_obj = @selector(insertObject:atIndex:);
        hac_swizzleInstanceMethod(hac_getClass(@"__NSA", @"rrayM"), sel_insert_obj, @selector(hac_insertObject:atIndex:));
        // 移除
        SEL sel_remove_obj = @selector(removeObjectAtIndex:);
        hac_swizzleInstanceMethod(hac_getClass(@"__NSA", @"rrayM"), sel_remove_obj, @selector(hac_removeObjectAtIndex:));
        // 替换
        SEL sel_replace_obj = @selector(replaceObjectAtIndex:withObject:);
        hac_swizzleInstanceMethod(hac_getClass(@"__NSA", @"rrayM"), sel_replace_obj, @selector(hac_replaceObjectAtIndex:withObject:));
        // 交换
        SEL sel_exchange_obj = @selector(exchangeObjectAtIndex:withObjectAtIndex:);
        hac_swizzleInstanceMethod(hac_getClass(@"__NSA", @"rrayM"), sel_exchange_obj, @selector(hac_exchangeObjectAtIndex:withObjectAtIndex:));
    });
}

#pragma mark - 取值越界防护

- (id)hac_objectAtIndex:(NSUInteger)index {
    if (index < 0 || index >= self.count) {
        return nil;
    }
    return [self hac_objectAtIndex:index];
}

#pragma mark - 其他出错防护

- (void)hac_addObject:(id)object {
    if (object) {
        [self hac_addObject:object];
    }
}

- (void)hac_addObjectsFromArray:(NSArray *)otherArray {
    if (otherArray && [otherArray isKindOfClass:NSArray.class]) {
        if (otherArray.count > 0) {
            [self hac_addObjectsFromArray:otherArray];
        }
    }
}

- (void)hac_insertObject:(id)anObject atIndex:(NSUInteger)index {
    if (anObject) {
        if (index >=0 && index <= self.count) {
            [self hac_insertObject:anObject atIndex:index];
        }
    }
}

- (void)hac_removeObjectAtIndex:(NSUInteger)index {
    if (index >=0 && index < self.count) {
        [self hac_removeObjectAtIndex:index];
    }
}

- (void)hac_replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
    if (index >=0 && index < self.count && anObject) {
        [self hac_replaceObjectAtIndex:index withObject:anObject];
    }
}

- (void)hac_exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2 {
    if (idx1 >=0 && idx1 < self.count && idx2 >=0 && idx2 < self.count ) {
        [self hac_exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2];
    }
}


@end
